<!--
 * @Description: 饼图
 * @Version: 1.0
 * @Autor: gaoluo
 * @Date: 2021-09-10 11:16:41
 * @LastEditors: gaoluo
 * @LastEditTime: 2021-12-06 11:53:56
 * @FilePath: /nhw/src/components/Echarts/EchartsPie/index.vue
-->
<template>
  <div class="echarts-pie-container">
    <div class="canvas-wrap" ref="container" v-show="data.length > 0"></div>
    <div class="error-msg" v-show="data.length <= 0">暂无数据</div>
  </div>
</template>

<script>
import echarts from "../index.js";
import { PieChart } from "echarts/charts";
echarts.use([PieChart]);
export default {
  name: "EchartsPie",
  components: {},
  props: {
    data: {
      type: Array,
      required: true,
    },
    roseType: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      PieChart: null,
    };
  },
  watch: {
    data() {
      this.renderCharts();
    },
  },
  mounted() {
    const dom = this.$refs.container;
    this.PieChart = echarts.init(dom);
    this.data.length > 0 && this.renderCharts();
  },

  methods: {
    renderCharts() {
      this.PieChart.setOption({
        tooltip: {
          trigger: "item",
        },
        legend: {
          width: 350,
          top: "bottom",
          left: "center",
          itemGap: 10,
          itemWidth: 15,
          itemHeight: 5,
          align: "left",
          textStyle: {
            color: "#999",
            padding: [5, 10],
          },
        },
        series: [
          {
            type: "pie",
            roseType: this.roseType,
            center: ["50%", "36%"],
            color: [
              "#A6B3E5",
              "#A6E5E3",
              "#7286C6",
              "#91CDFF",
              "#EDAF7E",
              "#F68F43",
            ],
            radius: ["26%", "56%"],
            label: {
              alignTo: "labelLine",
              formatter: "{d}%",
              color: "#333",
            },
            data: this.data,
          },
        ],
      });
    },
  },
};
</script>

<style lang="less" scoped>
.echarts-pie-container {
  width: 100%;
  .canvas-wrap {
    width: 100vw;
    height: 400px;
  }
}

.error-msg {
  text-align: center;
  line-height: 1.9;
  color: #ccc;
}
</style>